package com.realdolmen.whiteyellowcross.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Transaction;
import com.google.appengine.api.datastore.TransactionOptions;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;

@SuppressWarnings("serial")
public class MemCacheTest extends HttpServlet {

	


	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
		MemcacheService memCacheService = MemcacheServiceFactory.getMemcacheService();
		
		//create transction
		TransactionOptions transactionOptions = TransactionOptions.Builder.withXG(true);
		Transaction transacton = datastoreService.beginTransaction(transactionOptions);
		
		//entity creation
		Entity c1 = new Entity("Customer","nickname");
		c1.setProperty("name", "Frederik");
		c1.setProperty("age", "37");
		
		datastoreService.put(c1);
		
		Entity c2 = new Entity("Customer","nickname1");
		c2.setProperty("name", "Caro");
		c2.setProperty("age", "43");
		
		datastoreService.put(c2);
		
		Key key1 = c1.getKey();
		Key key2 = c2.getKey();
		
		memCacheService.put(key1, c1);
		memCacheService.put(key2, c2);
		
		transacton.commit();
		
		Entity memCustomer1 = (Entity)memCacheService.get(key1);
		Entity memCustomer2 = (Entity)memCacheService.get(key2);
		resp.getWriter().println("customer 1 name:" + memCustomer1.getProperty("name"));
		resp.getWriter().println("customer 2 name:" + memCustomer2.getProperty("name"));
		
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}
	

}